package com.future;

import org.apache.poi.hssf.model.InternalWorkbook;
import org.apache.poi.hssf.record.FileSharingRecord;
import org.apache.poi.hssf.record.WriteAccessRecord;
import org.apache.poi.hssf.record.WriteProtectRecord;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.GeneralSecurityException;

/**
 * 该版本jar包支持1.5JDK
 *
 * @author chenli
 * @create 2018-06-01
 */
public class EncryptTest {

    /**
     * 加密
     *
     * @param args :
     * @throws IOException ：
     */
    public static void main(String[] args) throws IOException {
        File file = new File("D://test14.xls");
        FileOutputStream fos=new FileOutputStream(file);
        InternalWorkbook internalWorkbook = InternalWorkbook.createWorkbook();
        HSSFWorkbook workbook=HSSFWorkbook.create(internalWorkbook);
        HSSFSheet sheet1 = workbook.createSheet("sheet1");
        workbook.writeProtectWorkbook("123456","admin");
//        下面是上面方法执行的代码，下方是有个setReadOnly方法，但是给任何值不起作用
//        int protIdx = -1;
//        FileSharingRecord frec = internalWorkbook.getFileSharing();
//        WriteAccessRecord waccess = internalWorkbook.getWriteAccess();
//        WriteProtectRecord wprotect = internalWorkbook.getWriteProtect();
//        frec.setReadOnly((short)0);
////        frec.setPassword(FileSharingRecord.hashPassword("123456"));
//        frec.setUsername("");
//        String s = frec.toString();
//        waccess.setUsername("");
        Row row1=sheet1.createRow(0);
        row1.createCell(0).setCellValue("aaaaa");
        row1.createCell(1).setCellValue("bbbbb");
        row1.createCell(2).setCellValue("ccccc");
        row1.createCell(3).setCellValue("ddddd");
        workbook.write(fos);
        fos.close();
    }

}